<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>js基础 | 茂茂个人博客</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="icon" href="https://pan.zealsay.com/blog/favicon.ico">
    <script>
          var _hmt = _hmt || [];
          (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?b0aae218897fa9d8a9f76e9a77e0b3c6";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
          })();
        </script>
    <meta name="description" content="开心最重要呀">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/blog/assets/css/0.styles.ed123d92.css" as="style"><link rel="preload" href="/blog/assets/js/app.e4cba77b.js" as="script"><link rel="preload" href="/blog/assets/js/5.1ad0ff24.js" as="script"><link rel="preload" href="/blog/assets/js/1.fe4eac58.js" as="script"><link rel="preload" href="/blog/assets/js/23.c3e9364b.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.58980e71.js"><link rel="prefetch" href="/blog/assets/js/11.461ee1ab.js"><link rel="prefetch" href="/blog/assets/js/12.de9aade4.js"><link rel="prefetch" href="/blog/assets/js/13.c1f7abfc.js"><link rel="prefetch" href="/blog/assets/js/14.eeee4da5.js"><link rel="prefetch" href="/blog/assets/js/15.225a74ee.js"><link rel="prefetch" href="/blog/assets/js/16.3eef12b1.js"><link rel="prefetch" href="/blog/assets/js/17.a97bd7c7.js"><link rel="prefetch" href="/blog/assets/js/18.461a74bd.js"><link rel="prefetch" href="/blog/assets/js/19.7b390d19.js"><link rel="prefetch" href="/blog/assets/js/20.8638de7c.js"><link rel="prefetch" href="/blog/assets/js/21.8b1e5c62.js"><link rel="prefetch" href="/blog/assets/js/22.b6b97b5b.js"><link rel="prefetch" href="/blog/assets/js/24.daa0ce9e.js"><link rel="prefetch" href="/blog/assets/js/25.0c44cdb2.js"><link rel="prefetch" href="/blog/assets/js/26.ed604668.js"><link rel="prefetch" href="/blog/assets/js/27.0c4acdea.js"><link rel="prefetch" href="/blog/assets/js/28.4f896280.js"><link rel="prefetch" href="/blog/assets/js/29.50e69b67.js"><link rel="prefetch" href="/blog/assets/js/3.90606e56.js"><link rel="prefetch" href="/blog/assets/js/30.5c69a6a2.js"><link rel="prefetch" href="/blog/assets/js/31.8a40c25d.js"><link rel="prefetch" href="/blog/assets/js/32.7a1f46ed.js"><link rel="prefetch" href="/blog/assets/js/33.a48429b4.js"><link rel="prefetch" href="/blog/assets/js/34.8f224bd0.js"><link rel="prefetch" href="/blog/assets/js/35.bc552eb5.js"><link rel="prefetch" href="/blog/assets/js/36.7e0cc0d9.js"><link rel="prefetch" href="/blog/assets/js/37.2bb21bfb.js"><link rel="prefetch" href="/blog/assets/js/38.f77b014f.js"><link rel="prefetch" href="/blog/assets/js/39.8a5ba8b0.js"><link rel="prefetch" href="/blog/assets/js/4.89c80be7.js"><link rel="prefetch" href="/blog/assets/js/40.c600c684.js"><link rel="prefetch" href="/blog/assets/js/41.0dbac397.js"><link rel="prefetch" href="/blog/assets/js/42.f1035a7b.js"><link rel="prefetch" href="/blog/assets/js/43.ace95e0e.js"><link rel="prefetch" href="/blog/assets/js/44.0ff283b9.js"><link rel="prefetch" href="/blog/assets/js/45.836151e7.js"><link rel="prefetch" href="/blog/assets/js/46.033824d7.js"><link rel="prefetch" href="/blog/assets/js/47.499ce120.js"><link rel="prefetch" href="/blog/assets/js/48.39c2db90.js"><link rel="prefetch" href="/blog/assets/js/49.ca266dff.js"><link rel="prefetch" href="/blog/assets/js/50.c12ee57e.js"><link rel="prefetch" href="/blog/assets/js/51.4c3c8a1c.js"><link rel="prefetch" href="/blog/assets/js/52.c11bc4a4.js"><link rel="prefetch" href="/blog/assets/js/53.d4dc4ec7.js"><link rel="prefetch" href="/blog/assets/js/54.300dd272.js"><link rel="prefetch" href="/blog/assets/js/55.6e86af02.js"><link rel="prefetch" href="/blog/assets/js/56.d91a04d8.js"><link rel="prefetch" href="/blog/assets/js/6.4f77b856.js"><link rel="prefetch" href="/blog/assets/js/7.db9a864b.js"><link rel="prefetch" href="/blog/assets/js/8.bf0cca83.js"><link rel="prefetch" href="/blog/assets/js/9.84ddd18e.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.ed123d92.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-0c5314fc><div data-v-0c5314fc><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-0c5314fc data-v-0c5314fc><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-5cb9a64e data-v-0c5314fc data-v-0c5314fc><h3 class="title" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e>茂茂个人博客</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><input type="password" value="" data-v-5cb9a64e> <span data-v-5cb9a64e>Konck! Knock!</span> <button data-v-5cb9a64e>OK</button></label> <div class="footer" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><span data-v-5cb9a64e><i class="iconfont reco-theme" data-v-5cb9a64e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-5cb9a64e>vuePress-theme-reco</a></span> <span data-v-5cb9a64e><i class="iconfont reco-copyright" data-v-5cb9a64e></i> <a data-v-5cb9a64e><span data-v-5cb9a64e>茂茂</span>
            
          <span data-v-5cb9a64e>2019 - </span>
          2023
        </a></span></div></div> <div class="hide" data-v-0c5314fc><div data-v-0c5314fc><div id="smart" class="wrapper-page" style="background-image:url(/blog/articleImage/img10.jpeg);background-position-x:center;background-position-y:center;background-size:cover;background-repeat-x:no-repeat;background-repeat-y:no-repeat;" data-v-0c5314fc><header class="navbar" data-v-0c5314fc><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/logo.png" alt="茂茂个人博客" class="logo"> <span class="site-name">茂茂个人博客</span></a> <div class="links"><div id="dayNightSwitch" class="generalWrapper" data-v-32f44868><a class="click" data-v-32f44868><div class="onOff daySwitch" data-v-32f44868><div class="star star1" data-v-32f44868></div> <div class="star star2" data-v-32f44868></div> <div class="star star3" data-v-32f44868></div> <div class="star star4" data-v-32f44868></div> <div class="star star5" data-v-32f44868></div> <div class="star sky" data-v-32f44868></div> <div class="sunMoon" data-v-32f44868><div class="crater crater1" data-v-32f44868></div> <div class="crater crater2" data-v-32f44868></div> <div class="crater crater3" data-v-32f44868></div> <div class="cloud part1" data-v-32f44868></div> <div class="cloud part2" data-v-32f44868></div></div></div></a></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/工具与阅读/" class="nav-link"><i class="iconfont undefined"></i>
  工具与阅读
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/vue/" class="nav-link"><i class="iconfont undefined"></i>
  vue
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/后端/" class="nav-link"><i class="iconfont undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/node/" class="nav-link"><i class="iconfont undefined"></i>
  node
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/IOS/" class="nav-link"><i class="iconfont undefined"></i>
  IOS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/WebGIS/" class="nav-link"><i class="iconfont undefined"></i>
  WebGIS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/React/" class="nav-link"><i class="iconfont undefined"></i>
  React
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/面试/" class="nav-link"><i class="iconfont undefined"></i>
  面试
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Angular/" class="nav-link"><i class="iconfont undefined"></i>
  Angular
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据结构与算法/" class="nav-link"><i class="iconfont undefined"></i>
  数据结构与算法
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/微前端架构/" class="nav-link"><i class="iconfont undefined"></i>
  微前端架构
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/blog/about/" class="nav-link"><i class="iconfont reco-account"></i>
  关于
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-0c5314fc></div> <aside class="sidebar" data-v-0c5314fc><div class="personal-info-wrapper" data-v-03833281 data-v-0c5314fc><img src="/blog/logo.png" alt="author-avatar" class="personal-img" data-v-03833281> <h3 class="name" data-v-03833281>
    茂茂
  </h3> <div class="num" data-v-03833281><div data-v-03833281><h3 data-v-03833281>43</h3> <h6 data-v-03833281>文章</h6></div> <div data-v-03833281><h3 data-v-03833281>30</h3> <h6 data-v-03833281>标签</h6></div></div> <hr data-v-03833281></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/工具与阅读/" class="nav-link"><i class="iconfont undefined"></i>
  工具与阅读
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/vue/" class="nav-link"><i class="iconfont undefined"></i>
  vue
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/后端/" class="nav-link"><i class="iconfont undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/node/" class="nav-link"><i class="iconfont undefined"></i>
  node
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/IOS/" class="nav-link"><i class="iconfont undefined"></i>
  IOS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/WebGIS/" class="nav-link"><i class="iconfont undefined"></i>
  WebGIS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/React/" class="nav-link"><i class="iconfont undefined"></i>
  React
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/面试/" class="nav-link"><i class="iconfont undefined"></i>
  面试
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Angular/" class="nav-link"><i class="iconfont undefined"></i>
  Angular
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据结构与算法/" class="nav-link"><i class="iconfont undefined"></i>
  数据结构与算法
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/微前端架构/" class="nav-link"><i class="iconfont undefined"></i>
  微前端架构
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/blog/about/" class="nav-link"><i class="iconfont reco-account"></i>
  关于
</a></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-5cb9a64e data-v-0c5314fc><h3 class="title" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e>js基础</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><input type="password" value="" data-v-5cb9a64e> <span data-v-5cb9a64e>Konck! Knock!</span> <button data-v-5cb9a64e>OK</button></label> <div class="footer" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><span data-v-5cb9a64e><i class="iconfont reco-theme" data-v-5cb9a64e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-5cb9a64e>vuePress-theme-reco</a></span> <span data-v-5cb9a64e><i class="iconfont reco-copyright" data-v-5cb9a64e></i> <a data-v-5cb9a64e><span data-v-5cb9a64e>茂茂</span>
            
          <span data-v-5cb9a64e>2019 - </span>
          2023
        </a></span></div></div></div> <div data-v-0c5314fc><main class="page" style="padding-right:0;"><div class="page-title" style="display:none;"><h1 class="title">js基础</h1> <div class="page-info" data-v-0efa1f05><i class="iconfont reco-account" data-v-0efa1f05><span data-v-0efa1f05>茂茂</span></i> <i class="iconfont reco-date" data-v-0efa1f05><span data-v-0efa1f05>2019-12-22</span></i> <i class="iconfont reco-eye" data-v-0efa1f05><span id="/blog/blogs/tec/2019/092102.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-0efa1f05><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-0efa1f05><span class="tag-item" data-v-0efa1f05>js</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="js"><a href="#js" class="header-anchor">#</a> js</h2> <h3 id="介绍js有哪些常用内置对象"><a href="#介绍js有哪些常用内置对象" class="header-anchor">#</a> 介绍js有哪些常用内置对象</h3> <ol><li>String(字符串),</li> <li>Number,</li> <li>Boolean（布尔对象）,</li> <li>Function(函数对象),</li> <li>Array(数组),</li> <li>Math,</li> <li>Object,</li> <li>RegExp（正则表达式对象）,</li> <li>Date(日期对象)，</li> <li>Error(异常对象)</li></ol> <h3 id="bom对象"><a href="#bom对象" class="header-anchor">#</a> bom对象</h3> <ol><li>window(核心)</li> <li>document对象</li> <li>location对象（当前页面地址）</li> <li>navigation对象（浏览器本身相关信息）</li> <li>screen对象（屏幕相关）</li> <li>history对象（浏览器历史相关）</li></ol> <h3 id="javascrip的一些编写原则"><a href="#javascrip的一些编写原则" class="header-anchor">#</a> JavaScrip的一些编写原则</h3> <ol><li>不要在同一行声明多个变量</li> <li>使用===或!==来比较</li> <li>使用字面量的方式来创建对象、数组，替代new Array这种形式</li> <li>switch语句必须要带default分支</li> <li>fon-in循环中的变量，用var关键字说明作用域，防止变量污染</li> <li>三元表达式可以替代if语句</li> <li>比较数据类型以下6中情况是false，其他都是true------false、&quot;&quot;、0、null、undefined、NaN</li> <li>数据类型检测用typeof，对象类型检测用instanceof 9、异步加载第三方的内容 10、单行注释//，多行注释/**/</li></ol> <h3 id="判断数据类型的方式以及各自的优缺点"><a href="#判断数据类型的方式以及各自的优缺点" class="header-anchor">#</a> 判断数据类型的方式以及各自的优缺点</h3> <p>一、typeof</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>               <span class="token comment">// number</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>            <span class="token comment">// boolean</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token string">'mc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment">// string</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">// function</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>              <span class="token comment">// object </span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>              <span class="token comment">// object</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>            <span class="token comment">// object</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token keyword">undefined</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// undefined</span>

</code></pre></div><p>优点：能够快速区分基本数据类型
缺点：不能将Object、Array和Null区分，都返回object</p> <p>二、instanceof</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token keyword">instanceof</span> <span class="token class-name">Number</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                    <span class="token comment">// false</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token boolean">true</span> <span class="token keyword">instanceof</span> <span class="token class-name">Boolean</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                <span class="token comment">// false </span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'str'</span> <span class="token keyword">instanceof</span> <span class="token class-name">String</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                <span class="token comment">// false  </span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token keyword">instanceof</span> <span class="token class-name">Array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                    <span class="token comment">// true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">instanceof</span> <span class="token class-name">Function</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">instanceof</span> <span class="token class-name">Object</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                   <span class="token comment">// true</span>
</code></pre></div><p>优点：能够区分Array、Object和Function，适合用于判断自定义的类实例对象
缺点：Number，Boolean，String基本数据类型不能判断
三、Object.prototype.toString.call()</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> toString <span class="token operator">=</span> <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>toString<span class="token punctuation">;</span>
 
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                      <span class="token comment">//[object Number]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                   <span class="token comment">//[object Boolean]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string">'mc'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                  <span class="token comment">//[object String]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                     <span class="token comment">//[object Array]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                     <span class="token comment">//[object Object]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment">//[object Function]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">undefined</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>              <span class="token comment">//[object Undefined]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                   <span class="token comment">//[object Null]</span>
</code></pre></div><p>优点：精准判断数据类型
缺点：写法繁琐不容易记，推荐进行封装后使用</p> <h3 id="dom对象与jquery对象转换"><a href="#dom对象与jquery对象转换" class="header-anchor">#</a> dom对象与jQuery对象转换</h3> <p>dom转jQuery dom对象如下：
var $dom = $(dom);</p> <p>jQuery对象转dom对象
var dom = $dom[0];
var dom = $dom.get(0);</p> <h3 id="正则相关-i不区分大小写-g匹配全部数据"><a href="#正则相关-i不区分大小写-g匹配全部数据" class="header-anchor">#</a> 正则相关[i不区分大小写，g匹配全部数据]</h3> <div class="language- extra-class"><pre class="language-text"><code>var str = &quot;Hello word!  I think word is good.&quot;;
替换str中的word为‘javascript’
str = str.replace(/word/ig, “javascript”);
判断str中是否包含word
/word/ig.test(str)
获取str中的所有word
str.match(/weaver/ig)
</code></pre></div><h3 id="js选择器"><a href="#js选择器" class="header-anchor">#</a> js选择器</h3> <div class="language- extra-class"><pre class="language-text"><code>选择select的值【假定select的id为slt】
$(&quot;#weaver option:selected&quot;).val()获取value $(&quot;#weaver
option:selected&quot;).text()//获取文本值

选择id为test的元素的文本内容
$(&quot;#test&quot;).val(); // jQuery(&quot;#test&quot;).val();
docunment.getElementById(&quot;test)
</code></pre></div><h3 id="jq的-function-与window-onload的区别"><a href="#jq的-function-与window-onload的区别" class="header-anchor">#</a> jq的$(function(){})与window.onload的区别</h3> <div class="language- extra-class"><pre class="language-text"><code>$(function(){})不会被覆盖，而window.onload会被覆盖（页面资源全部加载完毕后执行）
$(function(){})在window.onload执行前执行的
</code></pre></div><h3 id="dom文档加载步骤"><a href="#dom文档加载步骤" class="header-anchor">#</a> DOM文档加载步骤</h3> <ol><li>解析HTML结构</li> <li>加载外部的脚本和样式文件</li> <li>解析并执行脚本代码</li> <li>构造HTML DOM模型//执行ready</li> <li>加载图片等二进制资源</li> <li>页面加载完毕，执行window.onload</li></ol> <h3 id="作用域链"><a href="#作用域链" class="header-anchor">#</a> 作用域链</h3> <p>内部环境可以通过作用域链来访问外部环境的属性和方法，但是外部环境不能访问内部环境的任何属性和方法，只能通过定义函数来延长作用域链条</p> <h3 id="闭包"><a href="#闭包" class="header-anchor">#</a> 闭包</h3> <p>闭包就是能够读取其他函数内部变量的函数。由于在javascript语言中，只有函数内部的子函数才能读取局部变量，
因此可以把闭包简单的理解成“定义在一个函数内部的函数”。他的最大用处有两个，可以读取函数内部变量；让这些
变量的值始终保持在内存中</p> <div class="language- extra-class"><pre class="language-text"><code>for (var j = 0; j &lt; 2; j++) { 
		function f(){
			console.log(j)
		};
		f();
		setTimeout(()=&gt;{
			console.log(j);
		}, j); 
	}

	0 1 2 2
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>	var weaver = &quot;to cc百川？？？&quot;;
	var obj = {
		weaver:&quot;to cc百川！！！&quot;,
		fnobj:function(){ 
			fnobj1 = () =&gt; {
				console.log(this.weaver);
				fnobj2();
			}
			fnobj1();
			function fnobj2() {
				console.log(this.weaver)
			}
		}
	} 
	obj.fnobj();
	//结果： 
	to cc百川！！！
	to cc百川？？？
</code></pre></div><p>上面的代码在不使用箭头函数的情况下，this的指向是window所以是&quot;to cc百川？？？&quot;，</p> <h3 id="cookie-和session-的区别"><a href="#cookie-和session-的区别" class="header-anchor">#</a> cookie 和session 的区别</h3> <p>1、cookie数据存放在客户的浏览器上，session数据放在服务器上。</p> <p>2、cookie不是很安全，别人可以分析存放在本地的COOKIE并进行COOKIE欺骗</p> <p>考虑到安全应当使用session。</p> <p>3、session会在一定时间内保存在服务器上。当访问增多，会比较占用你服务器的性能</p> <p>考虑到减轻服务器性能方面，应当使用COOKIE。</p> <p>4、单个cookie保存的数据不能超过4K，很多浏览器都限制一个站点最多保存20个cookie。</p> <h3 id="给数组去重"><a href="#给数组去重" class="header-anchor">#</a> 给数组去重</h3> <div class="language- extra-class"><pre class="language-text"><code>var arrays = [1,2,3,2,4,6,5,7]
function unique (arrray) {
    var result=[]
    //请编程实现数组去重
    return result;
}	
//1.利用es6的set对象
result = [...new Set(array)];/[...new  Set (array)]
//2.利用对象属性的唯一性
let obj = {};
for (let i of array) {
    if (!obj[i]) {
        result.push(i);
        obj[i]=1;
    }
}
//3.利用sort方法排序去重
var newarray = array.sort();
result = [newarray[0]];
var length = newarray.length;
for( var i = 1 ;i&lt;length; i++) {
    newarray[i] !== newarray[i-1] &amp;&amp; result.push(newarray[i]);
}	
//4.利用for...of+includes()循环
for(var i of array) {
    !result.includes(i) &amp;&amp; result.push(i);
}
//5.用filter结合indexOf
result = array;
return result.filter((item, index)=&gt; {
    return result.indexOf(item) === index
})
//6.利用双层for循环（6分）类似
var length = array.length;
for(var i =0; i&lt;length; i++) {
var only = true;
for(var j =i+1; j&lt;length; j++){
    if(!only) continue;
    if(array[i] == array[j])  only = false; 
}
if(only) result.push(array[i]);
}

</code></pre></div><h3 id="统计字符串中字母个数"><a href="#统计字符串中字母个数" class="header-anchor">#</a> 统计字符串中字母个数</h3> <div class="language- extra-class"><pre class="language-text"><code>var str = &quot;I think javascript is good&quot;;
	//解：方法不唯一        
	str = str.toLowerCase();
    var result = {};
    for(var i in str) {
        if(str[i] in result) { 
            result[str[i]]++;
        } else { 
            result[str[i]] = 1;;
        }
    }
</code></pre></div><h3 id="js数据交互"><a href="#js数据交互" class="header-anchor">#</a> js数据交互</h3> <p>原生数据交互，post方法【可能用到方法或对象XMLHttpRequest、ActiveXObject、onreadystatechange、setRequestHeader】</p> <div class="language- extra-class"><pre class="language-text"><code>var xhr = null;
        try{
            xhr=new XMLHttpRequest();
        }catch(e){
            xhr=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
        } 
         xhr.open(&quot;post&quot;, url, true);
        //POST提交设置的协议头（GET方式省略）
        //这里需要通过服务器设置响应头解决跨域问题
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4){
                if(xhr.status==200){
                    console.log(&quot;success!!&quot;);
                }else{
                    console.log(&quot;error code &quot;+xhr.status)
                }
            }
        }
	   	//发送请求，post设置请求参数，get方式直接调用方法无需参数
        xhr.send(&quot;username=&quot;+name+&quot;&amp;age=&quot;+age);
</code></pre></div><p>Ajax</p> <div class="language- extra-class"><pre class="language-text"><code>$.ajax({
	type:'POST', // 规定请求的类型（GET 或 POST）
	url:uploadV, // 请求的url地址
	dataType:'json', //预期的服务器响应的数据类型 
	data:{},//规定要发送到服务器的数据
	beforeSend:function(){ //发送请求前运行的函数（发送之前就会进入这个函数）
	},
	success: function(result){ // 当请求成功时运行的函数
	},
	error:function(result){ //失败的函数
	},
	complete:function(){ //请求完成时运行的函数（在请求成功或失败之后均调用，即在 success 和 error 函数之后，不管成功还是失败 都会进这个函数）
	}
});
</code></pre></div><p>fetch调用fetch函数，传入接口url，然后返回一个promise（response）</p> <div class="language- extra-class"><pre class="language-text"><code>fetch('https://www.baidu.com')
  	.then(function(res) {
   		成功
  	},function(){
		失败
	});
</code></pre></div><h3 id="es6新特性"><a href="#es6新特性" class="header-anchor">#</a> es6新特性</h3> <ol><li>块级作用域变量let，const</li> <li>新增基本数据类型Symbol</li> <li>结构赋值</li> <li>给形参设置默认值&amp;箭头函数</li> <li>对象或者数组的展开符 …</li> <li>String的include方法</li> <li>Array新增API：isArray/from/of 和新增方法：entries()/kes()/values()等</li> <li>增加class语法糖</li> <li>新增模块化（import/export）</li> <li>新增Map，Set数据结构</li> <li>模板字符串</li> <li>for…of 和 for…in</li></ol> <h3 id="react或vue常用生命周期方法"><a href="#react或vue常用生命周期方法" class="header-anchor">#</a> react或vue常用生命周期方法</h3> <div class="language- extra-class"><pre class="language-text"><code>react：
		render()
		constructor()
		componentWillMount() 
		componentDidMount()
		shoudComponentUpdate()
		conponentWillReceiveProps()
		componentWillUpdate()
		componentDidUpdate ()
		componentWillUnmount ()
	vue（钩子函数）:
		beforeCreate()
		create()
		
		beforeMount()
		mountend()
		
		beforeUpdate()
		update()
		
		beforDestroy()
		destroy()
</code></pre></div><h3 id="原型链"><a href="#原型链" class="header-anchor">#</a> 原型链</h3> <p>原型：所有的函数都有一个特殊的属性prototype(原型)，prototype属性是一个指针，指向的是一个对象（原型对象），
原型对象中的方法和属性都可以被函数的实例所共享。所谓的函数的实例是指一个函数作为构造函数创建的对象，这些
对象实例都可以共享构造函数的原型的方法</p> <p>原型链：原型链是用于查找引用类型（对象）的属性，查找属性会沿着原型链依次进行，如果找到该属性会停止搜索并作
相应的操作，否则会沿着原型链依次查找结尾。常见应用是用在创建对象和继承上。</p></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2022/1/5 下午4:20:07</span></div></footer> <!----> <!----> <!----></main> <!----></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:190px;display:none;" data-v-5775ee02>
      我是王茂铭谢谢你的关注 
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-5775ee02></i></div> <canvas id="banniang" width="240" height="352" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><canvas id="vuepress-canvas-cursor"></canvas><APlayer audio="" fixed="true" mini="true" autoplay="autoplay" theme="#b7daff" loop="loop" order="random" preload="auto" volume="0.2" mutex="true" lrc-type="1" list-max-height="250" storage-name="vuepress-plugin-meting" id="aplayer-fixed"></APlayer></div></div>
    <script src="/blog/assets/js/app.e4cba77b.js" defer></script><script src="/blog/assets/js/5.1ad0ff24.js" defer></script><script src="/blog/assets/js/1.fe4eac58.js" defer></script><script src="/blog/assets/js/23.c3e9364b.js" defer></script>
  </body>
</html>
